<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import type { TableColumnCtx } from 'element-plus/es/components/table/src/table-column/defaults'
import { getAppPage, App } from '../../../api/application'
import dayjs from 'dayjs'

const searchForm = reactive({
  username: '',
  page: 1,
  page_size: 10
})

const tableData = ref<App[]>([])
const total = ref(0)

const onSearch = () => {
  getAppPage().then(res => {
    tableData.value = res.list
    total.value = res.total
  })
}

const formatter = (row: App, column: TableColumnCtx<App>) => {
  return dayjs(row.createTime * 1000).format('YYYY-MM-DD HH:mm:ss')
}
const handleUpdate = (row: App) => {
  console.log(row)
  console.log(window.location.origin + '/#/host/desktop?a=' + row.id)
  window.open(window.location.origin + '/#/host/desktop?a=' + row.id)
}

const handleDelete = (row: App) => {
  console.log(row)
  console.log(window.location.origin + '/#/host/monitor?a=' + row.id)
  window.open(window.location.origin + '/#/host/monitor?a=' + row.id)
}

onMounted(() => {
  onSearch()
})

</script>
<template>
    <el-card>
      <el-card class="search-card">
        <el-input v-model="searchForm.username" placeholder="请输入用户名称" />
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-card>

      <el-card class="body-card">
        <el-table :data="tableData" >
          <el-table-column type="index" width="50" />
          <el-table-column prop="name" label="应用"/>
          <el-table-column prop="alias" label="应用别名"/>
          <el-table-column prop="protocol" label="协议"/>
          <el-table-column prop="port" label="端口"/>
          <el-table-column
            prop="type"
            label="程序类型"
            width="100"
            >
            <template #default="scope">
              <el-tag
                :type="scope.row.is_desktop ? 'warning' : 'success'"
                disable-transitions
                >{{ scope.row.is_desktop?"应用" : "桌面" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="创建人"/>
          <el-table-column prop="create_time" label="创建时间" width="180" :formatter="formatter"/>
          <el-table-column label="操作" fixed="right">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="handleUpdate(scope.row)"
                ><el-icon><Edit /></el-icon> 打开</el-button
              >
              <el-button
                link
                type="primary"
                @click="handleDelete(scope.row)"
                ><el-icon><Edit /></el-icon> 监控</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination background layout="total,prev, pager, next" :total="total" />
      </el-card>

    </el-card>

</template>

<style lang="scss" scoped>
.search-card{
  .el-card__body{
    .el-input{
      width: 200px;
      margin-right: 10px;
    }
  }
  margin-bottom: 10px;
}
</style>
